package org.hard.wx;

import cn.hutool.json.JSONObject;
import org.hard.model.WxMssModel;
import org.hard.util.RestUtil;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

/**
 * @author: 王林博
 * @date: 2022年12月07日 15:00
 */
public class WxChatSendUtil {


    private static String wxCharUrl = "https://api.weixin.qq.com/cgi-bin/token";

    public static void main(String[] args) throws Exception {
        String appId = "wx3f2fe2de9b61cb05";
        String secret = "57c383643d37602c6924bb766bf3cece";

        getAccessToken(appId,secret);
    }


    public static String getAccessToken(String appId,String secret){
        String url = wxCharUrl+"?grant_type=client_credential&appid="
                + appId
                +"&secret="
                + secret;
        JSONObject jsonObject = RestUtil.get(url);
        System.out.println(jsonObject);
        return jsonObject.getStr("access_token");
    }

    /**
     *   发送微信小程序订阅消息
     * @author wubing
     * @date 2022/10/13 16:05
     * @param wxSendParam
     * @return java.lang.String
     */
    public static String WxChatSendOrderNotice(String appId, String secret, WxMssModel wxSendParam) {
        // 1、获取 接口调用凭证
        RestTemplate restTemplate = new RestTemplate();
        String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + getAccessToken(appId,secret);

        ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, wxSendParam, String.class);
        return responseEntity.getBody();

    }

}
